package com.example.supplychainbackend.service.Impl;

import com.example.supplychainbackend.controller.dto.enterprise.CertApplyProgressDto;
import com.example.supplychainbackend.controller.dto.enterprise.CertApplyStageDto;
import com.example.supplychainbackend.controller.dto.production.ProductionCertDto;
import com.example.supplychainbackend.controller.dto.supervise.ComplaintDto;
import com.example.supplychainbackend.controller.dto.supervise.EnterpriseCertApplyDto;
import com.example.supplychainbackend.controller.dto.supervise.ProductionCertApplyDto;
import com.example.supplychainbackend.controller.dto.supervise.TraceApplyDto;
import com.example.supplychainbackend.dao.production.ProductionProcessDao;
import com.example.supplychainbackend.dao.supervise.ComplaintDao;
import com.example.supplychainbackend.entity.production.ProductionProcessEntity;
import com.example.supplychainbackend.entity.supervise.ComplaintEntity;
import com.example.supplychainbackend.enums.enterprise.CertStage;
import com.example.supplychainbackend.enums.enterprise.ReviewStage;
import com.example.supplychainbackend.mapper.SuperviseMapper;
import com.example.supplychainbackend.service.EnterpriseService;
import com.example.supplychainbackend.service.ProductionService;
import com.example.supplychainbackend.service.SuperviseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SuperviseServiceImpl implements SuperviseService {
    private final ComplaintDao complaintDao;
    private final ProductionProcessDao productionProcessDao;
    private final SuperviseMapper mapper;
    private final EnterpriseService enterpriseService;
    private final ProductionService productionService;

    @Override
    public List<TraceApplyDto> listTraceApplys() {
        //查生产过程备案
        List<ProductionProcessEntity> processList = productionProcessDao.findAll();
        List<TraceApplyDto> results = new ArrayList<>();
        for(ProductionProcessEntity entity:processList){

            CertApplyProgressDto certDto = enterpriseService.getCertApplyProgress(Long.valueOf(entity.getEnterpriseId()));
            String eLicense =null;
            if (certDto!=null){
                if (0 == certDto.getStatus()){
                    eLicense = certDto.getLicense();
                }
            }
            results.add(TraceApplyDto.builder()
                    .productName(entity.getProductName())
                    .batch(entity.getBatch())
                    .manufacture(entity.getEnterpriseName())
                    .license(eLicense)
                    .certificate(entity.getCertificate())
                    .status(entity.getStatus())
                    .build()
            );
        }
        return results;
    }

    @Override
    public Integer reviewTraceApply(String id, int type) {
        ProductionProcessEntity entity = productionProcessDao.findByBatch(id).get();
        if(type==1) entity.setStatus(2);
        if(type==2) entity.setStatus(0);
        productionProcessDao.save(entity);
        return entity.getStatus();
    }

    @Override
    public ComplaintEntity createComplaint(ComplaintDto dto) {
        ComplaintEntity entity = mapper.toComplaintEntity(dto);
        entity = complaintDao.save(entity);
        return entity;
    }

    @Override
    public List<ComplaintDto> listComplaints() {
        List<ComplaintEntity> entities = complaintDao.findAll();
        return mapper.toComplaintDtoList(entities);
    }

    @Override
    public ComplaintDto reviewComplaint(String id) {
        ComplaintEntity entity = complaintDao.findById(Long.parseLong(id)).get();
        if(entity.getStatus() > 0)
            entity.setStatus(0);
        return mapper.toComplaintDto(entity);
    }

    @Override
    public List<EnterpriseCertApplyDto> listEnterpriseCertApplies() {
        List<CertApplyProgressDto> applyProgressDtoList = enterpriseService.listCertApplyProgress();
        List<EnterpriseCertApplyDto> resList = new ArrayList<>();
        for (CertApplyProgressDto cerApply:
             applyProgressDtoList) {
            resList.add(mapEnterpriseCertApply(cerApply));
        }
        return resList;
    }

    @Override
    public Long reviewEnterpriseCertApply(Long id, Integer type) {
        CertApplyProgressDto apply = enterpriseService.getCertApplyProgress(id);
        List<CertApplyStageDto> progress = apply.getProgress();
        switch (type) {
            case 1 -> {
                apply.setStatus(0);
                for (CertApplyStageDto stage :
                        progress) {
                    stage.setStatus(ReviewStage.PASSED.getDesc());
                }
            }
            case 2 -> {
                apply.setStatus(2);
                for (CertApplyStageDto stage :
                        progress) {
                    if (stage.getMessage().equals(CertStage.PRE_REVIEW.getDesc())) {
                        stage.setStatus(ReviewStage.FAILED.getDesc());
                    }
                }
            }
        }
        apply.setProgress(progress);
        enterpriseService.updateCertApplyProgress(id,apply);
        return id;
    }

    @Override
    public List<ProductionCertApplyDto> listProductionCertApplies() {
        List<ProductionCertDto> certList = productionService.getListProductionCert();
        List<ProductionCertApplyDto> resList = new ArrayList<>();
        for (ProductionCertDto cerApply:
                certList) {
            resList.add(mapProductionCertApply(cerApply));
        }
        return resList;
    }

    @Override
    public Long reviewProductionCertApply(Long id, Integer type) {
        int status = 1;
        switch (type) {
            case 1 -> status=0;
            case 2 -> status=2;
        }
        productionService.updateProductionCertApply(id,status);
        return id;
    }
    public EnterpriseCertApplyDto mapEnterpriseCertApply(CertApplyProgressDto dto){
        EnterpriseCertApplyDto res = EnterpriseCertApplyDto.builder().name(dto.getName()).idNumber(dto.getId())
                .trustNumber(dto.getTrustCode()).ownerPhoto(dto.getIdCard()).licensePhoto(dto.getLicense())
                .codePhoto(dto.getCode()).taxPhoto(dto.getTax()).status(dto.getStatus())
                .company(dto.getCompany()).companyId(String.valueOf(dto.getEid())).build();
        return res;
    }

    public ProductionCertApplyDto mapProductionCertApply(ProductionCertDto dto){
        ProductionCertApplyDto res = ProductionCertApplyDto.builder().productId(dto.getProductId())
                .productName(dto.getProductName()).fileImg(dto.getFileImg()).date(dto.getTime())
                .standard(dto.getStandard()).status(dto.getStatus())
                .leader(dto.getLeader()).phone(dto.getPhone()).build();
        return res;
    }
}
